<?php
function get_days($month, $year) {
	return cal_days_in_month(CAL_GREGORIAN, $month, $year);
}

function get_first_day($month, $year) {
	return get_day_of_week("01",$month,$year);
}

function get_day_of_week($day,$month,$year) {
	$date = date("N",strtotime("$year-$month-$day")) + 1;
	if ($date == 8) return 1;
	return $date;
}

function make_two($number) {
	if (strlen($number) == 1) return "0" . $number;
	else return $number;
}

function should_display_time($event=array(),$year,$month,$day) {
	if (	$event['startdate'] == "$year-$month-" . make_two($day)  && 
			!$event['allday'])	return true;
	return false;
}

function get_own_conflicts($event,$day,$month,$year) {
$estart = strtotime($event['startdate'] . " " . $event['starttime']);
	$eend = strtotime($event['enddate'] . " " . $event['endtime']);
	$conflicts = 0;
	foreach (get_regular_events($day,$month,$year) as $check) {
		$cstart = strtotime($check['startdate'] . " " . $check['starttime']);
		$cend = strtotime($check['enddate'] . " " . $check['endtime']);
		if (	($cstart >= $estart && 
				$cstart <= $eend) | 
				($cend >= $estart && 
				$cend <= $eend) |
				($estart >= $cstart &&
				$eend <= $cend )
			) {
				$conflicts2++;
				if ($conflicts2 > $conflicts) $conflicts = $conflicts2;
		}
	}
	return $conflicts;
}

function get_conflicts($event,$day,$month,$year,$recursive=1) {
	$estart = strtotime($event['startdate'] . " " . $event['starttime']);
	$eend = strtotime($event['enddate'] . " " . $event['endtime']);
	$conflicts = get_own_conflicts($event,$day,$month,$year);
	foreach (get_regular_events($day,$month,$year) as $check) {
		$cstart = strtotime($check['startdate'] . " " . $check['starttime']);
		$cend = strtotime($check['enddate'] . " " . $check['endtime']);
		if (	($cstart >= $estart && 
				$cstart <= $eend &&
				$event['id'] != $check['id']) | 
				($cend >= $estart && 
				$cend <= $eend &&
				$event['id'] != $check['id']) |
				($estart >= $cstart &&
				$eend <= $cend &&
				$event['id'] != $check['id'])
			) {
				if ($recursive == 1) {
					$conflicts2 = get_conflicts($check,$day,$month,$year,0);
				} else {
					$conflicts2++;

				}
				if ($conflicts2 > $conflicts) $conflicts = $conflicts2;
		}
	}
	if ($recursive == 1 && $conflicts > 1) return $conflicts -1;
	return $conflicts ;
}

function get_earliest_event($day,$month,$year) {
	$start = 9;
	$events = get_regular_events($day,$month,$year);
	foreach ($events as $event) {
		$time = explode_time_24($event['starttime']);
		if ($time['hour'] < $start) $start = $time['hour'];
	}
	return $start;
}

function time_to_pixels($hours,$minutes,$offset=0) {
		$loc = $loc + (($hours)*35); //Hours Offset
		if ($minutes > 0) {
			$loc = $loc + 15; //compensate for current hour
			$loc = $loc + (($minutes/15)*5); //Minutes offset
		}
		$loc = $loc - $offset;
	return $loc;
}

function get_time_difference( $start, $end ) {
 $uts['start'] = strtotime( $start );
 $uts['end'] = strtotime( $end );
 if( $uts['start']!==-1 && $uts['end']!==-1 ) {
 if( $uts['end'] >= $uts['start'] ) {
 $diff = $uts['end'] - $uts['start'];
 if( $days=intval((floor($diff/86400))) ) $diff = $diff % 86400;
 if( $hours=intval((floor($diff/3600))) ) $diff = $diff % 3600;
 if( $minutes=intval((floor($diff/60))) ) $diff = $diff % 60;
 $diff = intval( $diff );
 return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
 }
 else {
 trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING );
 }
 }
 else {
 trigger_error( "Invalid date/time data detected", E_USER_WARNING );
 }
 return( false );
 }
 
?>